In [1]:
import xgboost as xgb
import shap 
import numpy as np
import pandas as pd
import matplotlib.pylab as pl
shap.initjs()
In [2]:
#import XGBoost model from R into python
rmodel = xgb.Booster(model_file = 'Master.XGBModel')
In [3]:
#import data set into python
qsprdf = pd.read_csv("xgbpython.csv")

tracer = qsprdf['Tracer']
qspr = qsprdf.drop(['Tracer', 'Tracer_Conc'], axis=1)
In [4]:
#assign X and Y for predictions
Y = qsprdf['Tracer_Conc']
X = qspr
d_test = xgb.DMatrix(X, label=Y)
In [5]:
#for waterfall and forceplots
explainer = shap.TreeExplainer(rmodel)
shap_values = explainer(X)              
In [6]:
shap.force_plot(shap_values[56000:59999,:], out_names = 'tert-Butylbenzene') #tert-Butylbenzene forceplot for entire BTC 
shap.plots.force is slow for many thousands of rows, try subsampling your data.
Out[6]:
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.
In [7]:
shap.force_plot(shap_values[60000:63999,:], out_names = 'tert-Butylbenzene(2)') #tert-Butylbenzene(2) forceplot for entire BTC
Out[7]:
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.
In [8]:
shap.force_plot(shap_values[64000:67999,:], out_names = '1,2,4,5-Tetramethylbenzene') #1,2,4,5-Tetramethylbenzene forceplot for entire BTC 
Out[8]:
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.
In [9]:
shap.force_plot(shap_values[68000:71999,:], out_names = 'Pentamethylbenzene') #Pentamethylbenzene forceplot for entire BTC
Out[9]:
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.
In [10]:
shap.force_plot(shap_values[72000:75999,:], out_names = 'Pentamethylbenzene(2)') #Pentamethylbenzene(2) forceplot for entire BTC
Out[10]:
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.
In [ ]:
import os

os.system('jupyter nbconvert --to html yourNotebook.ipynb')